package com.hns.can.safety.service;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.safety.entity.BaseLineBlackRelEntity;
import com.hns.can.safety.vo.BaseLineBlackRelVo;
import com.hns.can.safety.vo.BlackSpotLineVo;
import com.hns.can.service.entity.BaseElectFencGpsInfoEntity;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 线路黑点分配
 *
 * @version Revision 2.0.0
 * @author:jeffxu
 * @email:835264047@qq.com
 */
@Service
public class BaseLineBlackRelService extends CommonService {

    @Autowired
    private BaseLineBlackRelHisService baseLineBlackRelHisService;

    @Autowired
    private FillCodeDscValue codeDscValue;

    /**
     * 获取线路分配黑点列表（分页）
     *
     * @param vo
     * @return
     */
    @LeftOrgAnnotation()
    @Transactional
    public Pagination searchBaseLineBlackRelPage(BaseLineBlackRelVo vo) {
        Pagination page = this.myBatisPage("baseLineBlackRel.getBaseLineBlackRelCnt",
                "baseLineBlackRel.getBaseLineBlackRelList", vo, BaseLineBlackRelEntity.class);
        return page;
    }

    /**
     * 变更黑点状态
     *
     * @param user
     * @param relId
     */
    @Transactional
    public void openCloseLineBlackRelById(SysUserEntity user, String relId) {
        BaseLineBlackRelEntity entity = this.commonEntityDao.find(relId, BaseLineBlackRelEntity.class);
        entity.setUpdateUser(user.getId());
        entity.setUpdateTime(new Date());

        if ("0".equals(entity.getBlackSt())) {//启用
            entity.setBlackSt("1");

            baseLineBlackRelHisService.addBaseLineBlackRelHis(user, relId);
        } else {//停用
            entity.setBlackSt("0");

            baseLineBlackRelHisService.uptBaseLineBlackRelHisDsablDate(user, relId);
        }

        this.commonEntityDao.update(entity);
    }

    /**
     * 添加线路分配黑点
     *
     * @param vo
     * @param user
     */
    @Transactional
    public void addBaseLineBlackRel(BaseLineBlackRelVo vo, SysUserEntity user) {
        String lineId = vo.getLineId();
        String[] rcrdIds = vo.getRcrdIds();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        for (int i = 0; i < rcrdIds.length; i++) {
            BaseLineBlackRelEntity entity = new BaseLineBlackRelEntity();
            String relId = UtilString.getUUID();

            entity.setRelId(relId);
            entity.setCorpId(user.getCorpId());
            entity.setLineId(lineId);
            entity.setBlackId(rcrdIds[i]);
            entity.setBlackSt("1");
            entity.setMnpltDate(sdf.format(new Date()));
            entity.setCrtUser(user.getId());
            entity.setCrtTime(new Date());
            entity.setUpdateUser(user.getId());
            entity.setUpdateTime(new Date());

            this.commonEntityDao.save(entity);
            baseLineBlackRelHisService.addBaseLineBlackRelHis(user, relId);
        }
    }

    /**
     * 获取线路分配黑点列表
     * 用于导出数据
     *
     * @param vo
     * @return
     */
    @LeftOrgAnnotation()
    @Transactional
    public List<BaseEntity> getBaseLineBlackRelList(BaseLineBlackRelVo vo) {
        List<BaseEntity> list = PubUtil.convertListMapToObject(
                sqlMapDao.selectList("baseLineBlackRel.getBaseLineBlackRelList4Exp", vo), BaseLineBlackRelEntity.class);
        return list;
    }

    /**
     * 获取线路下的所有黑点信息
     *
     * @param vo the vo
     * @return the black spot by line id
     * @author # xujf（835264047@qq.com）
     * @date # 2016-12-26 14:35:25
     */
    @Transactional
    public List<BaseEntity> getBlackSpotByLineId(BaseLineBlackRelVo vo) {
        //获取线路下的所有黑点信息
        List<BaseEntity> list1 = PubUtil.convertListMapToObject(
                sqlMapDao.selectList("baseLineBlackRel.getBlackSpotByLineId", vo), BlackSpotLineVo.class);

        if (PubUtil.isEmpty(list1)) {
            return null;
        } else {
            packageMapAreaInfo(list1);
            return list1;
        }
    }


    /**
     * 获取选中的黑点信息
     *
     * @param ids the ids
     * @return the black spots by ids
     * @author # xujf（835264047@qq.com）
     * @date # 2016-12-26 14:35:35
     */
    public List<BaseEntity> getBlackSpotsByIds(String[] ids) {
        //获取选中的黑点信息
        List<BaseEntity> list1 = PubUtil.convertListMapToObject(
                sqlMapDao.selectList("baseLineBlackRel.getBlackSpotsByIds", ids), BlackSpotLineVo.class);

        if (PubUtil.isEmpty(list1)) {
            return null;
        } else {
            packageMapAreaInfo(list1);
            return list1;
        }
    }

    /**
     * 获取相关黑点的地图区域信息
     *
     * @param list1 the list 1
     * @author # xujf（835264047@qq.com）
     * @date # 2016-12-26 14:34:07
     */
    private void packageMapAreaInfo(List<BaseEntity> list1) {
        //获取黑点的地图区域信息
        List<String> fencIds = new ArrayList<String>();
        for (int i = 0; i < list1.size(); i++) {
            BlackSpotLineVo vo1 = (BlackSpotLineVo) list1.get(i);
            if ("1".equals(vo1.getFencType())) {//多边形
                fencIds.add(vo1.getFencId());
            }
        }

        if (PubUtil.isNotEmpty(fencIds)) {
            List<BaseElectFencGpsInfoEntity> list2 = PubUtil.convertListMapToObject(
                    sqlMapDao.selectList("baseLineBlackRel.getBlackSpotFencGps", fencIds), BaseElectFencGpsInfoEntity.class);

            //封装数据
            //1、封装黑点和经纬度信息
            if (PubUtil.isNotEmpty(list2)) {
                //封装
                BlackSpotLineVo vo1;
                List<BaseElectFencGpsInfoEntity> list3;

                for (int i = 0; i < list1.size(); i++) {
                    vo1 = (BlackSpotLineVo) list1.get(i);

                    list3 = new ArrayList<BaseElectFencGpsInfoEntity>();

                    for (BaseElectFencGpsInfoEntity vo2 : list2) {
                        if (vo1.getFencId().equals(vo2.getFencId())) {
                            list3.add(vo2);
                        }
                    }

                    vo1.setList(list3);
                }
            }
        }

        //2、解析代码描述信息
        Map<String, String> map = new HashMap<String, String>();
        map.put("blackNature", "BLAK0000");
        codeDscValue.fillCodeDscValue(list1, map);
    }

}